package org.apache.ibatis.common.logging.log4j2;

import org.apache.ibatis.common.logging.Log;
import org.apache.ibatis.common.logging.LogFactory;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.spi.AbstractLogger;
import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;

/**
 * @Name Log4j2AbstractLoggerImpl
 * @Description: Log4jImpl 具体实现类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-05-15 15:42:23
 **/
public class Log4j2AbstractLoggerImpl implements Log {
    private static final Marker MARKER = MarkerManager.getMarker(LogFactory.MARKER);
    private static final String FQCN = Log4j2Impl.class.getName();
    private final ExtendedLoggerWrapper logger;

    public Log4j2AbstractLoggerImpl(AbstractLogger abstractLogger) {
        this.logger = new ExtendedLoggerWrapper(abstractLogger, abstractLogger.getName(), abstractLogger.getMessageFactory());
    }

    @Override
    public boolean isDebugEnabled() {
        return logger.isDebugEnabled();
    }

    @Override
    public boolean isTraceEnbaled() {
        return logger.isTraceEnabled();
    }

    @Override
    public void error(String msg, Throwable e) {
        logger.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(msg), e);
    }

    @Override
    public void error(String msg) {
        logger.logIfEnabled(FQCN, Level.ERROR, MARKER, (Message) new SimpleMessage(msg), null);
    }

    @Override
    public void warn(String msg) {
        logger.logIfEnabled(FQCN, Level.WARN, MARKER, (Message) new SimpleMessage(msg), null);
    }

    @Override
    public void debug(String msg) {
        logger.logIfEnabled(FQCN, Level.DEBUG, MARKER, (Message) new SimpleMessage(msg), null);
    }

    @Override
    public void trace(String msg) {
        logger.logIfEnabled(FQCN, Level.TRACE, MARKER, (Message) new SimpleMessage(msg), null);
    }
}

